"use strict";
const BaseController = require("../base");

// 定义创建接口的请求参数规则
const getSpuListRule = {
  page: "number",
  limit: "number",
};

const saveSpuRule = {
  category3Id: "string",
  description: "string",
  spuImageList: {
    type: "array",
    itemType: "object",
    required: true,
    rule: {
      id: "string",
      imgName: "string",
      imgUrl: "string",
    },
  },
  spuName: "string",
  spuSaleAttrList: {
    type: "array",
    itemType: "object",
    required: true,
    rule: {
      baseSaleAttrId: "string",
      saleAttrName: "string",
      spuSaleAttrValueList: {
        type: "array",
        itemType: "object",
        required: true,
        rule: {
          isChecked: "string",
          saleAttrValueName: "string",
        },
      },
    },
  },
  tmId: "string",
};

const updateSpuRule = {
  ...saveSpuRule,
  id: "string",
  spuSaleAttrList: {
    type: "array",
    itemType: "object",
    required: true,
    rule: {
      id: {
        type: "string",
        required: false,
      },
      baseSaleAttrId: "string",
      saleAttrName: "string",
      spuSaleAttrValueList: {
        type: "array",
        itemType: "object",
        required: true,
        rule: {
          id: {
            type: "string",
            required: false,
          },
          isChecked: "string",
          saleAttrValueName: "string",
        },
      },
    },
  },
};

const removeSpuRule = {
  id: "string",
};

class SpuController extends BaseController {
  async getSpuList() {
    const ctx = this.ctx;
    // 如果参数校验未通过，将会抛出一个 status = 422 的异常
    const { page, limit } = ctx.params;
    const params = {
      page: +page,
      limit: +limit,
    };
    ctx.validate(getSpuListRule, params);
    const data = await ctx.service.product.spu.getSpuList(params);
    this.success(data);
  }
  async saveSpu() {
    const ctx = this.ctx;
    // 校验参数是否符合我们预期的格式
    // 如果参数校验未通过，将会抛出一个 status = 422 的异常
    const body = ctx.request.body;
    ctx.validate(saveSpuRule, body);
    const data = await ctx.service.product.spu.saveSpu(body);
    this.success(data);
  }
  async updateSpu() {
    const ctx = this.ctx;
    const body = ctx.request.body;
    ctx.validate(updateSpuRule, body);
    const success = await ctx.service.product.spu.updateSpu(body);
    if (success) {
      this.success();
    } else {
      this.fail();
    }
  }
  async removeSpu() {
    const ctx = this.ctx;
    // 校验参数是否符合我们预期的格式
    const id = ctx.params.id;
    // 如果参数校验未通过，将会抛出一个 status = 422 的异常
    ctx.validate(removeSpuRule, {
      id,
    });
    const success = await ctx.service.product.spu.removeSpu(id);
    if (success) {
      this.success();
    } else {
      this.fail();
    }
  }
  async getSpuImageList() {
    const ctx = this.ctx;
    // 校验参数是否符合我们预期的格式
    const id = ctx.params.spuId;
    // 如果参数校验未通过，将会抛出一个 status = 422 的异常
    ctx.validate(removeSpuRule, {
      id,
    });
    const data = await ctx.service.product.spu.getSpuImageList(id);
    this.success(data);
  }
}

module.exports = SpuController;
